/*
 * @lc app=leetcode.cn id=4 lang=golang
 *
 * [4] 寻找两个正序数组的中位数
 */

// @lc code=start
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	m := mergeArray(nums1, nums2)
	c := len(m)
	i := c / 2
	if c%2 == 0 {
		return float64(m[i]+m[i-1]) / 2
	} else {
		return float64(m[i])
	}
}

func mergeArray(nums1, nums2 []int) []int {
	i, j := 0, 0
	tmp := make([]int, 0)
	for i < len(nums1) && j < len(nums2) {
		if nums1[i] < nums2[j] {
			tmp = append(tmp, nums1[i])
			i++
		} else {
			tmp = append(tmp, nums2[j])
			j++
		}
	}

	tmp = append(tmp, nums1[i:]...)
	tmp = append(tmp, nums2[j:]...)

	return tmp
}

// @lc code=end

